package com.wizardOther.Items;

import com.appclient.DaoInstanceEnum;
import com.carpark.ejb.Model.AccountingdataModel;
import com.carpark.ejb.Model.CarsModel;
import com.carpark.ejb.Model.ClientsModel;
import com.carpark.ejb.Model.ParkingpassesModel;
import com.table.ClientTableController2;
import com.wizardOther.OtherControlledScreen;
import com.wizardOther.OtherScreensController;
import java.io.IOException;
import java.net.URL;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;


/**
 *
 * @author Vi
 */
public class ClientController extends ValidationForm implements Initializable, OtherControlledScreen {

    private OtherScreensController myController;

    @FXML 
    private Button cancel;
    @FXML
    private Button findClient;
    @FXML
    private TextField name;
    @FXML
    private TextField lastName;
    @FXML
    private TextField city;
    @FXML
    private TextField zipCode;
    @FXML
    private TextField street;
    @FXML
    private TextField house;
    @FXML
    private TextField local;
    @FXML
    private TextField nip;
    @FXML
    private TextField phone;
    @FXML
    private TextField email;
    @FXML
    private TextArea comment;
    
    
    @FXML
    private TableView<CarsModel> carTable;
    @FXML
    private TableColumn<CarsModel, String> plateNumber;
    @FXML
    private TableColumn<CarsModel, String> made;
    @FXML
    private TableColumn<CarsModel, String> model;
    @FXML
    private TableColumn<CarsModel, String> carType;
    @FXML
    private TableColumn<CarsModel, String> comments;
    
    @FXML
    private TableView<ParkingpassesModel> passesTable;
    @FXML
    private TableColumn<ParkingpassesModel, String> number;
    @FXML
    private TableColumn<ParkingpassesModel, String> startDate;
    @FXML
    private TableColumn<ParkingpassesModel, String> endDate;
    @FXML
    private TableColumn<ParkingpassesModel, String> tariff;
    @FXML
    private TableColumn<ParkingpassesModel, String> type;
    
    @FXML
    private TableView<AccountingdataModel> accTable;
    @FXML
    private TableColumn<AccountingdataModel, String> name2;
    @FXML
    private TableColumn<AccountingdataModel, String> city2;
    @FXML
    private TableColumn<AccountingdataModel, String> zipCode2;
    @FXML
    private TableColumn<AccountingdataModel, String> street2;
    @FXML
    private TableColumn<AccountingdataModel, String> house2;
    @FXML
    private TableColumn<AccountingdataModel, String> local2;  
    @FXML
    private TableColumn<AccountingdataModel, String> nip2;
    @FXML
    private TableColumn<AccountingdataModel, String> phone2;
    @FXML
    private TableColumn<AccountingdataModel, String> type2;
    @FXML
    private TableColumn<AccountingdataModel, String> comment2;
    @FXML
    private Tab clientTab;
    @FXML
    private Tab carTab;
    @FXML
    private Tab passTab;
    @FXML
    private Tab accTab;
  
    private ObservableList<CarsModel> carData = FXCollections.observableArrayList();
    private ObservableList<ParkingpassesModel> passData = FXCollections.observableArrayList();
    private ObservableList<AccountingdataModel> accData = FXCollections.observableArrayList();

    private ArrayList<Node> inputs = new ArrayList<Node>();
    private Integer selectedClient;
    private ClientTableController2 controller;

    
    
    @FXML
    private void handleButtonAction(ActionEvent event) {
      
        if(event.getSource()==findClient){
            showClientTable();
            selectedClient=controller.getSelectedClient();
            
            if(this.selectedClient!=null){ 
                clearTable();
                setTab(false);
                loadData(selectedClient);
                loadCars(selectedClient);
                loadPasses(selectedClient);
                loadAcc(selectedClient);
                
                checkEmptyTab();
            }
       }
       
       if(event.getSource()==cancel){
                clearForm();
                clearTable();
                setTab(true);
       }
        
    }
    
    private void clearTable(){
          carData.clear();
          accData.clear();
          passData.clear();
                
                
    }
    
    private void checkEmptyTab(){
        if(carData.isEmpty()){
            carTab.setDisable(true);
        }
        if(accData.isEmpty()){
            accTab.setDisable(true);
        }
        if(passData.isEmpty()){
            passTab.setDisable(true);
        }
    }
    
    private void loadAcc(Integer client){
        ClientsModel c = new ClientsModel(client);
        List<AccountingdataModel> data = DaoInstanceEnum.INSTANCE.getDao().getAccountingDataByClient(c);
        
        for(AccountingdataModel p : data){
            p.setPayerType(renamePeyerTypeToPolish(p.getPayerType()));
            accData.add(p);
        }
    }
    
    private void loadPasses(Integer client){
        ClientsModel c = new ClientsModel(client);
        List<ParkingpassesModel> data = DaoInstanceEnum.INSTANCE.getDao().getPassesByClient(c);
        
        for(ParkingpassesModel p : data){
            p.setPassType(renamePassesTypeToPolish(p.getPassType()));
            p.setDateIssue(Date.valueOf(new SimpleDateFormat("yyyy-MM-dd").format(p.getDateIssue())));
            p.setExpirationDate(Date.valueOf(new SimpleDateFormat("yyyy-MM-dd").format(p.getExpirationDate())));
            passData.add(p);
        }
    }
    
    private void loadCars(Integer client){
        
        ClientsModel c = new ClientsModel(client);
          List<CarsModel> data = DaoInstanceEnum.INSTANCE.getDao().getCarsByClient(c);
      
         for (CarsModel car : data) {
                car.setCarType(renameCarTypeToPolish(car.getCarType()));
                carData.add(car);
            }
    }
    
    
    
    private void clearForm(){
        this.selectedClient=null;
        this.name.setText("");
        this.lastName.setText("");
        this.city.setText("");
        this.zipCode.setText("");
        this.street.setText("");
        this.house.setText("");
        this.local.setText("");
        this.phone.setText("");
        this.email.setText("");
        this.comment.setText("");
        this.nip.setText("");
    }
    
    private void showClientTable(){
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("clientTable2.fxml"));
            AnchorPane page = (AnchorPane) loader.load();
            Stage dialogStage = new Stage();
            dialogStage.setTitle("Wybierz klienta");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            Scene scene = new Scene(page);
            dialogStage.setScene(scene);

             controller = loader.getController();
             controller.setDialogStage(dialogStage);
         
            dialogStage.showAndWait();

        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    
    private void loadData(Integer client){
        ClientsModel c = DaoInstanceEnum.INSTANCE.getDao().getClientById(client);
          if (c != null) {
                this.name.setText(c.getName());
                this.lastName.setText(c.getLastName());
                this.city.setText(c.getCity());
                this.zipCode.setText(c.getZipCode());
                this.street.setText(c.getStreet());
                this.house.setText(c.getHouseNumber());
                if(c.getLocalNumber()!=null){
                    this.local.setText(c.getLocalNumber().toString());     
                }
                this.nip.setText((c.getNipNumber()));
                this.phone.setText(c.getPhone());
                this.email.setText(c.getEmail());
                this.comment.setText(c.getComments());
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        inputs.add(name);
        inputs.add(lastName);
        inputs.add(city);
        inputs.add(zipCode);
        inputs.add(street);
        inputs.add(house);
        inputs.add(local);
        inputs.add(nip);
        inputs.add(phone);
        inputs.add(email);
        inputs.add(comment);
        
        name.setEditable(false);
        lastName.setEditable(false);
        city.setEditable(false);
        zipCode.setEditable(false);
        street.setEditable(false);
        house.setEditable(false);
        local.setEditable(false);
        nip.setEditable(false);
        phone.setEditable(false);
        email.setEditable(false);
        comment.setEditable(false);
        
        if(this.selectedClient==null){
            setTab(true);
        }
        
        plateNumber.setCellValueFactory(
                new PropertyValueFactory<CarsModel, String>("numberPlate"));
        made.setCellValueFactory(
                new PropertyValueFactory<CarsModel, String>("make"));
        model.setCellValueFactory(
                new PropertyValueFactory<CarsModel, String>("model"));
        carType.setCellValueFactory(
                new PropertyValueFactory<CarsModel, String>("CarType"));
        comments.setCellValueFactory(
                new PropertyValueFactory<CarsModel, String>("comments"));

        carTable.setItems(carData);
        
       number.setCellValueFactory(
                new PropertyValueFactory<ParkingpassesModel, String>("numberId"));
       startDate.setCellValueFactory(
                new PropertyValueFactory<ParkingpassesModel, String>("dateIssue"));
       endDate.setCellValueFactory(
                new PropertyValueFactory<ParkingpassesModel, String>("expirationDate"));
       type.setCellValueFactory(
                new PropertyValueFactory<ParkingpassesModel, String>("PassType"));
       tariff.setCellValueFactory(
                new PropertyValueFactory<ParkingpassesModel, String>("tarrif"));

        passesTable.setItems(passData);
        
        name2.setCellValueFactory(
                new PropertyValueFactory<AccountingdataModel, String>("name"));
        city2.setCellValueFactory(
                new PropertyValueFactory<AccountingdataModel, String>("city"));
        zipCode2.setCellValueFactory(
                new PropertyValueFactory<AccountingdataModel, String>("zipNumber"));
        street2.setCellValueFactory(
                new PropertyValueFactory<AccountingdataModel, String>("street"));
        house2.setCellValueFactory(
                new PropertyValueFactory<AccountingdataModel, String>("houseNumber"));
        local2.setCellValueFactory(
                new PropertyValueFactory<AccountingdataModel, String>("localNumber"));
        nip2.setCellValueFactory(
                new PropertyValueFactory<AccountingdataModel, String>("nipNumber"));
        phone2.setCellValueFactory(
                new PropertyValueFactory<AccountingdataModel, String>("phone"));
        type2.setCellValueFactory(
                new PropertyValueFactory<AccountingdataModel, String>("payerType"));
        comment2.setCellValueFactory(
                new PropertyValueFactory<AccountingdataModel, String>("comments"));
        
        accTable.setItems(accData);
    }

    @Override
    public void setScreenParent(OtherScreensController screenParent) {
        myController = screenParent;
    }

    private void setTab(boolean b){
            carTab.setDisable(b);
            passTab.setDisable(b);
            accTab.setDisable(b);
           
    }
   
}
